package cn.gjxblog.aop.autoproxy.demo2;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * 采用 DefaultAdvisorAutoProxyCreator 自动代理创建器（和Advisor有关）
 */
@Configuration
@ComponentScan("cn.gjxblog.aop.autoproxy")
public class AutoProxyConfig {

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        return new DefaultAdvisorAutoProxyCreator();
    }

    @Bean
    public AfterReturningAdvice afterReturningAdvice(){
        AfterReturningAdvice afterReturningAdvice = new AfterReturningAdvice() {
            @Override
            public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
                System.out.println("afterReturningAdvice~~");
                System.out.println(returnValue);
                System.out.println(method.getName());
                System.out.println(Arrays.toString(args));
                System.out.println(target.getClass());
                System.out.println(target);
            }
        };
        return afterReturningAdvice;
    }

    @Bean
    public NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor(){
        NameMatchMethodPointcutAdvisor nameMatchMethodPointcutAdvisor = new NameMatchMethodPointcutAdvisor();
        nameMatchMethodPointcutAdvisor.addMethodName("getUser");
        nameMatchMethodPointcutAdvisor.setAdvice(afterReturningAdvice());
        return nameMatchMethodPointcutAdvisor;
    }
}
